/**
 * Created by HSH on 2016/3/18.
 */
'use strict';
define(function (require) {
    var angular = require('angular');
    /**
     * Change 'ui-router' configuration, making it more structured
     */
    var registerDialogBoolean = true;
    var registerFunction = function (param) {
        return function () {
            return param
        };
    };
    var initDefaultStateList = [];
    var blockStateList = [];
    var blockDialogList = [];
    ePart.app = {};
    ePart.router = {
        globalStateList: [],

        globalModule: function () {
            return angular.module(ePart.Configure.ALL_MODULE_NAME, ePart.Configure.INJECTION_MODULE())
        },

        run: function () {
            return ePart.router.globalModule().run(['$rootScope', '$state', '$stateParams', function ($rootScope, $state, $stateParams) {
                $rootScope.webAppName = ePart.Configure.WEB_APP_NAME;
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
                $rootScope.$on('$viewContentLoaded', function () {
                    $('html, body').animate({
                        scrollTop: 0
                    }, 200);
                });
            }
            ]);
        },

        configure: function () {
            ePart.router.run().config(function ($stateProvider, $urlRouterProvider, $uiViewScrollProvider) {
                $uiViewScrollProvider.useAnchorScroll();
                $urlRouterProvider.otherwise('/index');
                $stateProvider.state(ePart.router.initState().fullName, ePart.router.initState().viewsObject);
                _(ePart.router.initBlockState()).each(function (initBlockItem) {
                    $stateProvider.state(initBlockItem.fullName, initBlockItem.viewsObject);
                });

            });
        },

        initialLayout: function (initParams) {
            initDefaultStateList.push(initParams);
        },

        defaultLayout: function (defaultParams) {
            this.extractController(defaultParams);
            initDefaultStateList.push(defaultParams);
        },

        blockLayout: function (blockParams) {
            this.extractController(blockParams);
            blockStateList.push(blockParams);
        },

        initState: function () {
            var initFullObject = {
                    fullName: ePart.Configure.DEFAULT_URL_REDIRECTION
                },
                initStateObject = {
                    url: '/' + ePart.Configure.DEFAULT_URL_REDIRECTION
                },
                views = {};
            _(initDefaultStateList).each(function (stateItem, index) {
                if (stateItem.stateName === ePart.Configure.DEFAULT_URL_REDIRECTION) {
                    if (index === 0) {
                        views[''] = {template: stateItem.template}
                    } else {
                        views[stateItem.target] = {template: stateItem.template}
                    }
                }
            });
            initStateObject['views'] = views;
            initFullObject['viewsObject'] = initStateObject;
            return initFullObject;
        },

        initBlockState: function () {
            return _.map(blockStateList, function (blockItem) {
                var targetObject = {};
                targetObject[blockItem.target] = {template: blockItem.template};
                return {
                    fullName: blockItem.stateName,
                    viewsObject: {
                        url: blockItem.stateUrl,
                        views: targetObject
                    }
                }
            });
        },

        extractController: function (blockOptions) {
            if (blockOptions && blockOptions.controller && blockOptions.controller !== '') {
                ePart.router.blockController(blockOptions);
            }
        },

        blockController: function (options) {
            var mainController = function ($scope, ngDialog, iptvService, $location, $stateParams, $state) {
                if (registerDialogBoolean) {
                    ePart.pcUtils.initDialog = registerFunction(ngDialog);
                    ePart.pcUtils.location = registerFunction($location);
                    ePart.pcUtils.stateUrl = registerFunction($state);
                    ePart.app.iptv = iptvService;
                    registerDialogBoolean = false;
                }
                options.controller.controller($scope, $stateParams);
            };
            ePart.Configure.MODULE_CONTROLLER().controller(options.controllerModule, mainController);
        },

        blockDialog: function (dialogOptions) {
            var initDialog = {
                dialogName: dialogOptions.dialogName,
                template: dialogOptions.template || '',
                controller: dialogOptions.controller.service,
                overlay: dialogOptions.overlay || true,
                disableAnimation: dialogOptions.disableAnimation || false,
                showClose: dialogOptions.showClose || true,
                closeByEscape: dialogOptions.closeByEscape || true,
                plain:dialogOptions.plain || true
            };
            blockDialogList.push(initDialog);
            return blockDialogList
        },

        start: function () {
            this.configure();
        }
    };
    /*
     * pc utils dialogs
     * @param dialogName
     * @param scope
     */
    ePart.pcUtils.dialog = function (dialogName, scope) {
        var dialogOptionsObject = _.find(blockDialogList, function (blockDialog) {
            return blockDialog.dialogName === dialogName
        });
        dialogOptionsObject.scope = scope;
        if (arguments[2]) {
            dialogOptionsObject.preCloseCallback = arguments[2]
        }
        ePart.pcUtils.initDialog().open(dialogOptionsObject);
    };
    ePart.pcUtils.closeDialog = function () {
        ePart.pcUtils.initDialog().close();
    };
});